<template>
    <orh-table reload  @on-reload="reload" add="add" @on-add="moralAdd" del="del" @on-del="delConfirm" pages :pageDate="pageDate" @on-page-change="reload">
        
        <Table border stripe :columns="columns" @on-selection-change="selectOne" :data="data" :loading="tableload" slot="table" ref="tables"></Table>
        
        <orh-sidebar v-model="show" :title="titles" scale slot="others">
            <component :is="content" :condition="condition" @requery="requery"></component>
        </orh-sidebar>

        <Button type="info" slot="returntosuperior" style="margin-left: 10px;" @click="searchModal = true" size="small">查找</Button>

        <Button type="info" slot="returntosuperior" style="margin-left: 10px;" @click="exportCurrent" size="small">导出当前数据</Button>
        <Button type="info" slot="returntosuperior" style="margin-left: 10px;" @click="exportModal = true" size="small">选择导出数据</Button>

        <Modal v-model="urlState" title="现场照片" slot="others">
            <p><img :src="imgUrl" width="100%"></p>
        </Modal>
        
        <Modal v-model="searchModal" title="选择查询条件" :loading="true" @on-ok="searchClick" slot="others" :width="600">
            <search-criteria :modalStatus="searchModal" @on-search="searchSubmit" :types="12" ref="searchRef"></search-criteria>
        </Modal>
        
        <Modal v-model="exportModal" title="选择导出条件" :loading="true" @on-ok="exportClick" slot="others" :width="600">
            <search-criteria :modalStatus="exportModal" @on-search="exportSubmit" :types="12" exportData ref="exportRef"></search-criteria>
        </Modal>
        
    </orh-table>
</template>
                
<script>
export default {
    name: 'dutymodule-lateselfstudy',
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);},
        SearchCriteria: resolve => {require(['$m/DutyModule/DutyData/SearchCriteria.vue'], resolve);}
    },
    data () {
        return {
            searchModal: false,
            exportModal: false,

            imgUrl: null,
            urlState:false,

            show: false,
            content: null,
            titles: null,
            tableload: true,
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 0
            },
            data: [],
            columns: [
                {
                    type: 'selection',
                    minWidth: 60,
                    align: 'center',
                },
                {
                    title: '班级信息',
                    align: 'center',
                    children: [
                        {
                            title: '年级',
                            minWidth: 150,
                            align: 'center',
                            render: (h, params) => {
                                let name = params.row.grade.grade_name.zh
                                return h('div', name)
                            }
                        },
                        {
                            title: '班级',
                            minWidth: 100,
                            align: 'center',
                            render: (h, params) => {
                                let name = params.row.class?params.row.class.eclass_name.zh:''
                                return h('div', name)
                            }
                        },
                        {
                            title: '实到人数',
                            minWidth: 100,
                            align: 'center',
                            key: 'classnumber',
                        },
                        {
                            title: '值班老师',
                            minWidth: 200,
                            align: 'center',
                            render: (h, params) => {
                                let name = params.row.user_ids_name !=null?params.row.user_ids_name.teacher_name.zh:''
                                return h('div', name)
                            }
                        },
                        {
                            title: '值班自习',
                            minWidth: 150,
                            align: 'center',
                            render: (h, params) => {
                                let name = params.row.types == '2'?'第一晚自习':params.row.types == '3'?'第二晚自习':params.row.types == '4'?'第三晚自习':''
                                return h('div', name)
                            }
                        },
                        {
                            title: '值班时间',
                            minWidth: 150,
                            align: 'center',
                            key: 'created_at',
                        },
                        {
                            title: '值班领导',
                            minWidth: 150,
                            align: 'center',
                            render: (h, params) => {
                                let name = params.row.user_ids_name?params.row.user_ids_name.teacher_name.zh:''
                                return h('div', name)
                            }
                        },
                    ]
                },
                {
                    title: '是否确认',
                    minWidth: 100,
                    align: 'center',
                    render: (h, params) => {
                        let name = params.row.section == '1'? '未确认':'已确认'
                        let color = params.row.section == '1'? '#ed3f14':'#47cb89'
                        return h('div', {
                            style: {
                                color: color
                            }
                        }, name)
                    }
                },
                {
                    title: '细则',
                    align: 'center',
                    children: [
                        {
                            title: '细则状态',
                            minWidth: 100,
                            align: 'center',
                            render: (h, params) => {
                                let name = params.row.rules.classification
                                return h('div', {
                                    style: {
                                        color: name == 1?'#19be6b':'#ed3f14',
                                    }
                                }, name == 1?'加分':'减分')
                            }
                        },
                        {
                            title: '细则信息',
                            minWidth: 200,
                            align: 'center',
                            render: (h, params) => {
                                let name = params.row.rules.entry_content.zh
                                return h('div', name)
                            }
                        },
                    ]
                },
                {
                    title: '现场图片',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        if(params.row.img != null) {
                            return h('div', [
                                h('img', {
                                    attrs: {
                                        src: params.row.img
                                    },
                                    style: {
                                        width:'100px',
                                        height:'40px',
                                        verticalAlign: 'middle',
                                    },
                                    on: {
                                        click: () => {
                                            this.picture(params.row.img)
                                        }
                                    }
                                }),
                            ]);
                        }else{
                            return h('div', '暂无图片');
                        }
                    }
                },
                {
                    title: '操作',
                    key: 'action',
                    minWidth: 150,
                    align: 'center',
                    fixed: 'right',
                    render: (h, params) => {
                        let type = params.row.section == '1'? false:true
                        return h('div', [
                            h('Button', {
                                directives: [
                                    {
                                        name: 'button',
                                        value: 'edit',
                                    }
                                ],
                                props: {
                                    type: 'primary',
                                    size: 'small',
                                    disabled: type
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.UpdateData(this.data[params.index].watchlist_column_id)
                                    }
                                }
                            }, '编辑'),
                            h('Poptip', {
                                props: {
                                    confirm: true,
                                    transfer: true,
                                    title: '您确定要删除这条数据吗?',
                                    type: 'error',
                                    size: 'small',
                                    placement:'left-end'
                                },
                                style: {
                                    textAlign: 'left',
                                },
                                on: {
                                    'on-ok': () => {
                                        this.remove(this.data[params.index].watchlist_column_id)
                                    }
                                }
                            }, [
                                h('Button', {
                                    directives: [
                                        {
                                            name: 'button',
                                            value: 'del',
                                        }
                                    ],
                                    props: {
                                        type: 'error',
                                        size: 'small',
                                        disabled: type
                                    }
                                }, '删除')
                            ]),
                        ]);
                    }
                },
            ],
            url: 'api/watchlist/v1/watchlistfromdatas/list/12',
            delUrl: 'api/watchlist/v1/watchlistfromdatas/del/',

            del: null,
            condition: null,
            condnum: null,

            exportUrl: 'api/watchlist/v1/get/datas/exportExceltontsta',
            exportColumns: [
                {
                    title: '年级',
                    key: 'grade_name'
                },
                {
                    title: '班级',
                    render: (h, params) => {
                        return h('div', params.row.eclass_name.zh?params.row.eclass_name.zh:params.row.eclass_name.mn);
                    }
                },
                {
                    title: '实到人数',
                    key: 'classnumber'
                },
                {
                    title: '值班老师',
                    render:(h,params)=> {
                        return h('span',params.row.teacher_name?params.row.teacher_name.zh?params.row.teacher_name.zh:params.row.teacher_name.mn:'')
                    }
                },
                {
                    title: '值班自习',
                    key: 'types'
                },
                {
                    title: '值班时间',
                    key: 'created_at'
                },
                {
                    title: '值班领导',
                    key: 'teacher_names'
                },
                {
                    title: '是否确认',
                    key: 'section'
                },
                {
                    title: '细则状态',
                    key: 'classification'
                },
                {
                    title: '细则信息',
                    render:(h,params)=> {
                        return h('span',params.row.entry_content.zh?params.row.entry_content.zh:params.row.entry_content.mn)
                    }
                },
                {
                    title: '备注',
                    render:(h,params)=> {
                        return h('span',params.row.remarks?params.row.remarks.zh?params.row.remarks.zh:params.row.remarks.mn:'')
                    }
                }
            ]
        }
    },
    methods: {
        
        /************************************************************
         * 
         * 查询数据
         * 
         ************************************************************/
        searchClick () {
            let searchCondition = this.$refs.searchRef.searchCondition
            searchCondition ()
        },
        
        /************************************************************
         * 
         * 提交查询条件
         * @param   {data}              条件
         * 
         ************************************************************/
        searchSubmit (data) {
            this.condnum = data
            this.condnum['page'] = 1
            this.reload ()
        },
        
        /************************************************************
         * 
         * 导出当前数据
         * 
         ************************************************************/
        exportClick () {
            let searchCondition = this.$refs.exportRef.searchCondition
            searchCondition ()
        },
        
        /************************************************************
         * 
         * 提交导出条件
         * @param   {data}              条件
         * 
         ************************************************************/
        exportSubmit (data) {
            this.$http.get(this.exportUrl, { params: data }).then(val=>{
                window.open(val.data.url, '_blank')
                this.exportModal = false
            }).catch(err => {
                this.exportModal = false
            })
        },
        
        /************************************************************
         * 
         * 导出当前数据
         * 
         ************************************************************/
        exportCurrent () {
            let time = this.$util.dateTime()
            this.$refs.tables.exportCsv({
                filename: `早自习教务值班${time}`,
                columns: this.exportColumns,
                data: this.data.map((el, i) => this.exportHandel(el, i))
            });
        },
        exportHandel (el, i) {
            let obj = {
                grade_name: el.grade.grade_name,
                eclass_name: el.class.eclass_name,
                classnumber: el.classnumber,
                teacher_name: el.dutyteacher_name ? el.dutyteacher_name.teacher_name:'',
                types: el.types == '1'?'早自习':el.types == '5'?'早读(上)':el.types == '7'?'早读(下)':el.types == '6'?'自愿值班':'',
                created_at: el.created_at,
                teacher_names: el.user_ids_name?el.user_ids_name.teacher_name:'',
                section: el.section == '1'? '未确认':'已确认',
                classification: el.rules.classification == 1?'加分':'减分',
                entry_content: el.rules.entry_content,
                remarks: el.remarks,
            }
            return obj
        },
        
        /************************************************************
         * 
         * 删除确认
         * 
         ************************************************************/
        delConfirm () {
            if (this.del.length) {
                this.$Modal.confirm({
                    title: '删除确认',
                    closable: true,
                    loading: true,
                    content: '您确定要删除这些数据吗?',
                    onOk: () => {
                        this.delSubmit ()
                    }
                })
            }
        },
        
        /************************************************************
         * 
         * 查看图片
         * 
         ************************************************************/
        picture (src) {
            this.imgUrl = src
            this.urlState = true
        },
        
        /************************************************************
         * 
         * 添加数据
         * 
         ************************************************************/
        moralAdd () {
            this.condition = false
            this.titles = '添加数据'
            this.show = true
            this.content = 'Operation'
        },
        
        /************************************************************
         * 
         * 单条删除数据
         * 
         ************************************************************/
        remove (index) {
            this.del = []
            this.del.push(index)
            this.delSubmit()
        },
        
        /************************************************************
         * 
         * 批量删除数据
         * 
         ************************************************************/
        selectOne(index){
            this.del = []
            index.forEach(item => {
                this.del.push(item.watchlist_column_id)
            });
        },
        
        /************************************************************
         * 
         * 执行删除操作
         * 
         ************************************************************/
        delSubmit () {
            this.del = JSON.stringify(this.del)
            this.$http.delete(this.delUrl+this.del).then(val=>{
                this.reload (this.pageDate.current_page)
                this.$Modal.remove()
            }).catch(err => {
                this.$Modal.remove()
            })
        },
        
        /************************************************************
         * 
         * 当添加或修改数据后刷新数据
         * 
         ************************************************************/
        requery (state) {
            if(state == true){
                this.show = false
                this.reload (this.pageDate.current_page)
            }
        },
        
        /************************************************************
         * 
         * 修改数据
         * 
         ************************************************************/
        UpdateData (index) {
            this.condition = index
            this.titles = '修改数据'
            this.show = true
            this.content = 'Operation'
        },
        
        /************************************************************
         * 
         * 手动刷新数据
         * @param   {index}             分页
         * 
         ************************************************************/
        reload (index) {
            this.condnum.page = index?index:1
            this.data = []
            this.tableload = true
            this.queryData()
        },
        
        /************************************************************
         * 
         * 请求列表数据
         * 
         ************************************************************/
        queryData () {
            this.tableload = true
            this.$http.get(this.url, { params: this.condnum }).then(val=>{
                this.pageDate.per_page = val.pages.per_page
                this.pageDate.total = val.pages.total
                this.pageDate.current_page = val.pages.current_page
                this.data = val.data
                this.tableload = false
                this.searchModal = false
            }).catch(err => {
                this.tableload = false
                this.searchModal = false
            })
        },
    },
    created () {
        this.condnum = { page: 1 }
        this.reload ()
    },
};
</script>

